//
//  MyViewController.swift
//  HoneyBee
//
//  Created by point on 2018/1/26.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import SwiftyJSON
private let kCellID = "kCellID"

class MyViewController: UIViewController {
    @IBOutlet weak var top: NSLayoutConstraint!
    
    @IBOutlet weak var tableView: UITableView!
    var modelArr:[String] = []
    fileprivate var headView: MyHeadView! = nil
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       // self.top.constant =  AdaptationTools.shareInstance.mpNavigationHeight()
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
            
        }
        setupUI()
        modelArr = ["我的收藏","消息通知","管理机构","用户反馈","系统设置","我的预约","我的钱包"]
    }
}

extension MyViewController: MyHeadViewDelegate{
    func clickName() {
        
        let alertController = UIAlertController(title: "昵称修改",
                                                message: "请输入昵称", preferredStyle: .alert)
        alertController.addTextField {
            (textField: UITextField!) -> Void in
            textField.placeholder = "昵称"
        }
        
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "确定", style: .default, handler: {
            action in
            
            let login = alertController.textFields!.first!
            self.headView.nameTitle.text = login.text
            
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    
    func clickHead() {
        let uploadAlertController = UIAlertController(title:nil, message: nil, preferredStyle:UIAlertController.Style.actionSheet)
        
        let takePhoto = UIAlertAction(title:"拍照", style:UIAlertAction.Style.default) { (action:UIAlertAction)in
            self.openCamera()
            
        }
        let photoLib = UIAlertAction(title:"从相册选择", style:UIAlertAction.Style.default) { (action:UIAlertAction)in
            self.openPhoto()
        }
        
        let cancel = UIAlertAction(title:"取消", style:UIAlertAction.Style.cancel) { (action:UIAlertAction)in
        }
        
        uploadAlertController.addAction(takePhoto)
        uploadAlertController.addAction(photoLib)
        uploadAlertController.addAction(cancel)
        present(uploadAlertController, animated: true, completion: nil)
    }
}

// MARK:-  相机 相册 代理
extension MyViewController:UIImagePickerControllerDelegate ,UINavigationControllerDelegate {
    func openCamera(){
        let pick:UIImagePickerController = UIImagePickerController()
        pick.delegate = self
        pick.sourceType = UIImagePickerController.SourceType.camera
        pick.allowsEditing = true
        present(pick, animated: true, completion: nil)
    }
    
    func openPhoto(){
        let pick:UIImagePickerController = UIImagePickerController()
        pick.delegate = self
        pick.sourceType = UIImagePickerController.SourceType.photoLibrary
        pick.allowsEditing = true
        present(pick, animated: true, completion: nil)
        
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true, completion: nil)
        self.hudLoad()
         guard var img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else { return }
        img = img.rotate(aImage: img)
        img = img.square(aImage: img)
        let data = img.jpegData(compressionQuality: 0.5)
        let dict = ["sign":"111","type":"0"];
        NetworkTools.shareInstance.AFUpload(boolImage: true, URLString: kuploadimg, parameters: dict, data: data!, progressing: { (progress) in
        }) { (result, success) in
            self.hudHide()
            let json = JSON(result)
            let headStr:String = json["data"].string!
            print(headStr);
            self.headView.headImageView.showImageS(headStr)
            
        }
    }
    
    
}

// MARK:- 原生代理
extension MyViewController:UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! MyTableViewCell
        
        
        cell.configModel(modelArr: modelArr, index: indexPath)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60;
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let head = MyHeadView.loadFromNib()
        self.headView = head
        head.delegate = self
        return head
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.item == 2 {
            let vc = SchoolManageViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
}

// MARK:- 设置UI
extension MyViewController {
    fileprivate func setupUI(){
        self.navigationItem.title = "我的"
        tableView.register(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: kCellID)
        tableView.separatorStyle = .none
    }
}
